<?php
/**
 * Created by PhpStorm.
 * User: JinLei
 * Date: 2018/1/29
 * Time: 15:31
 * 调查问卷
 */
namespace Home\Controller\Index;
use Home\Controller\BaseController;
use Home\Model\QuestionModel;
use Home\Model\SurveyinfoModel;
use Home\Model\SurveyuserModel;
use Home\Model\UseranswerModel;

class IndexController extends BaseController {

    /**
     * 显示调查问卷页面
     */
    public function index(){
        if(IS_GET){
            $id = I("get.id");
            if(isset($id) && is_numeric($id)){
                $result = $this->getOneQuest($id);
                if($result == -5){
                    echo '该调查问卷访问无效！';
                    die;
                }else{
                    $require = $this->getRequire($id);
                    $this->assign('require',$require);
                    $this->assign('result',$result);
                    $this->display("Index/index");
                }
            }else{
                echo '该调查问卷非法！';
                die;
            }
        }
    }

    /**
     * @param $id
     * @return mixed
     * 获取单个问卷
     */
    public function getOneQuest($id){
        $model = new SurveyinfoModel();
        return $model->getInfo($id);
    }

    /**
     * @return string
     *
     * 获取并处理post数据
     */
    public function getData(){
        $post = (I('post.'));
        if(!$this->isMobile($post['iphone'])){
            return '手机号格式不正确!';
        }
        $id = $this->userInsert($post);
        if($id>0){
            $res = $this->answerInsert($post,$id);
            if($res == 1){
                $this->display("Index/success");
            }
        }
    }

    /**
     * @param $post
     * @return mixed
     *向surveyuser表中插入数据
     *
     **/

    public function userInsert($post){
        $user['surveyid'] = $post['sid'];
        $user['usermobile'] = $post['iphone'];
        $user['etr_date'] = date("Y-m-d H:i:s");
        $user['etr_ip'] = get_client_ip();
        $model = new SurveyuserModel();
        return $model->insert($user);
    }

    /**
     * @param $post
     * @param $id
     * @return int
     * 向useranswer表中插入数据
     */
    public function answerInsert($post,$id){
        $userModel = new UseranswerModel();
        $i = 0;
        foreach ($post as $k=>$v){
            if(is_numeric($k)){
                $answer['userid'] = $id;
                $answer['questionid'] = $k;
                if(is_array($v)){
                    $answer['answercontent'] = implode(',',$v);
                }else{
                    $answer['answercontent'] = $v;
                }
                $answer['etr_date'] = date('Y-m-d H:i:s');
                $answer['etr_ip'] = get_client_ip();
                $i++;
                $res[] = $userModel->insert($answer);
            }
        }
        if(count($res) == $i){
            return 1;
        }

    }


    /**
     * 向surveyinfo中增加访问记录
     */
    public function updateAccess(){
        $user['surveyid'] = I('post.id');
        $model = new SurveyinfoModel();
        $res = $model->updatePageview($user);
        $this->ajaxReturn($res);
    }

    /**
     * @param $id
     * @return string
     * 获取必填项值
     */
    public function getRequire($id){
        $model = new QuestionModel();
        $rid = $model->getRequire($id);
        foreach ($rid as $k => $v){
            $require[$k]['id'] = $v['questionid'];
            $require[$k]['type'] = $v['questiontype'];
        }
        $require = json_encode($require);
        return $require;
    }
}